<?php

class Consulting extends CI_Controller {

    private static $layout_data = array();
    private static $home_data = array();
    
    function __construct()
    {
        parent::__construct();

        $this->load->model('node_model');
    }
	
    function index()
    {
        $limit = 4;
        $offset = 0;

        self::$home_data['goctamtinh'] = $this->node_model->getListNodeByCategoryID(23, $limit, $offset);
        self::$home_data['cauhoicuaban'] = $this->node_model->getListNodeByCategoryID(22, $limit, $offset);
        self::$home_data['vanpham'] = $this->node_model->getListNodeByCategoryID(24, $limit, $offset);
        self::$home_data['tulieu'] = $this->node_model->getListNodeByCategoryID(26, $limit, $offset);

        self::$layout_data['page_title'] = 'Oneway.vn - Tư Vấn';
        self::$layout_data['content'] = $this->load->view('consulting/index', self::$home_data, TRUE);
        $this->load->view('layouts/main', self::$layout_data);
    }

    /**
     * Get new consulting ajax
     */
    public function getBlockHomeConsulting()
    {
        //Set params
        $categoryID = $this->input->get('categoryID')?$this->input->get('categoryID'):23;
        $limit = $this->input->get('limit')?$this->input->get('limit'):5;
        $offset = $this->input->get('offset')?$this->input->get('offset'):0;

        //Get arrNode data and total
        $arrNode = $this->node_model->getListNodeByCategoryID($categoryID, $limit, $offset);

        //Check arrNode
        if($arrNode['total'] > 0)
        {
            //Get newest node data
            $firstNode = $this->node_model->getNodeDetail($arrNode['data'][0]['node_id']);
        }

        //Set home data
        self::$home_data['firstNode'] = $firstNode;
        self::$home_data['arrNode'] = array_slice($arrNode['data'], 1);

        //Assign to view
        $this->load->view('consulting/consulting_block', self::$home_data);
    }

    /**
     * Get new consulting ajax
     */
    public function getBlockHomeQuestion()
    {
        //Set params
        $categoryID = $this->input->get('categoryID')?$this->input->get('categoryID'):22;
        $limit = $this->input->get('limit')?$this->input->get('limit'):5;
        $offset = $this->input->get('offset')?$this->input->get('offset'):0;

        //Get arrNode data and total
        $arrNode = $this->node_model->getListNodeByCategoryID($categoryID, $limit, $offset);

        self::$home_data['arrNode'] = $arrNode['data'];

        //Assign to view
        $this->load->view('consulting/question_block', self::$home_data);
    }

    /**
     * View detail function
     */
    public function view()
    {
        //Get nodeID
        $nodeID = $this->uri->segment(4);

        //Get note detail
        $nodeDetail = $this->node_model->getNodeDetail($nodeID);

        //Assign to view
        self::$home_data['node'] = $nodeDetail;
        self::$layout_data['page_title'] = $nodeDetail['title'];
        self::$layout_data['page_description'] = $nodeDetail['description'];
        self::$layout_data['page_thumb'] = $nodeDetail['thumb'];
        self::$layout_data['content'] = $this->load->view('consulting/view', self::$home_data, TRUE);

        //Load view
        $this->load->view('layouts/main', self::$layout_data);
    }
    
    /**
     * Ask question action
     */
    public function askQuestion()
    {
        //Get params
        $fullname = $this->input->post('fullname');
        $sender = $this->input->post('sender');
        $content = $this->input->post('content');
        $reciver = 'chiase@oneway.vn';
        
        //Set response default
        $response = array(
            'error'     => 1,
            'message'   => 'Vui lòng điền đầy đủ thông tin.'
        );

        if(empty($fullname) || empty($sender) || empty($content) || empty($reciver))
        {
            echo json_encode($response); exit();
        }

        $this->load->helper('email');

        if (!valid_email($sender) || !valid_email($reciver))
        {
            $response['message'] = 'Email không hợp lệ';
            echo json_encode($response); exit();
        }

        $this->load->library('email');

        $this->email->from($sender, $fullname);
        $this->email->to($reciver);
        $this->email->subject('Oneway.vn - Câu hỏi của bạn');
        $this->email->message($content);
        
        $result = $this->email->send();

        //Set response
        $response = array(
            'error'     => $result?0:1,
            'message'   => $result?'Success':'Error'
        );

        //Return data
        echo json_encode($response); exit();
    }

}
